import req from '@/utils/request'
import type {
  GetUserListApiParams,
  GetUserListApiResponseType,
  UserSaveApiParams,
  UserRolesDataType,
  UserUpdateApiParams,
} from './model/userControllerApiModel'

enum Api {
  usetList = '/admin/acl/user',
  userSave = '/admin/acl/user/save',
  toAssign = '/admin/acl/user/toAssign',
  doAssign = '/admin/acl/user/doAssign',
  updateUser = '/admin/acl/user/update',
  removeUser = '/admin/acl/user/remove',
  batchRemove = '/admin/acl/user/batchRemove',
}

// 获取用户列表
export const getUserListApi = ({
  page,
  limit,
  username,
  name,
}: GetUserListApiParams) => {
  return req.get<null, GetUserListApiResponseType>(
    `${Api.usetList}/${page}/${limit}`,
    {
      params: {
        username,
        name,
      },
    }
  )
}

// 新增用户
export const userSaveApi = (userInfo: UserSaveApiParams) => {
  return req.post(Api.userSave, userInfo)
}

// 获取角色数据
export const getUserRolesApi = (id: number) => {
  return req.get<null, UserRolesDataType>(`${Api.toAssign}/${id}`)
}

// 根据用户分配角色
export const assignRolesToUserApi = (id: number, roleId: string[]) => {
  return req.post<null, null>(`${Api.doAssign}?userId=${id}&roleId=${roleId}`)
}

// 修改用户
export const updateUserApi = (data: UserUpdateApiParams) => {
  return req.put(Api.updateUser, data)
}

// 删除用户
export const deleteUserApi = (id: number) => {
  return req.delete<null, null>(`${Api.removeUser}/${id}`)
}

// 批量删除
export const batchRemoveApi = (idList: number[]) => {
  console.log(idList)
  return req.delete<null, null>(Api.batchRemove, { data: idList })
}
